﻿#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
autovfd.py v1.00 by [[zh:user:Shizhao]]

license: GPL

"""
import os
import sys, time, datetime
import wikipedia, config,catlib
site=wikipedia.getSite()

def vfdm():
    """
    过期月份，应该提交删除
    """
    if int(time.strftime("%m",time.gmtime()))==1:
        vfdm=12
    else:
        vfdm=int(time.strftime("%m",time.gmtime()))-1
    vfdm=str(vfdm)
    return vfdm  


def templatetime(timestamp):
    """
    格式化页面放置模板的时间
    """
    t=time.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
    y=time.strftime("%Y",t)
    m=time.strftime("%m",t)
    d=time.strftime("%d",t)
    h=time.strftime("%H",t)
    min=time.strftime("%M",t)
    s=time.strftime("%S",t)
    temptime=datetime.datetime(int(y),int(m),int(d),int(h),int(min),int(s))	
    return temptime	

def vfd(category, templates):

#得到过期条目列表


    """
    得到某个分类下没有提交删除的条目,根据设置一个测试页面的链入，判断是否已经提交删除
    """
    n=0
    vfdlist=[]
    cat=catlib.Category(site, category)
    listOfArticles = cat.articlesList()

    test=wikipedia.Page(site,u'User:Sz-iwbot/vfdall')

    pagelist = [page for page in listOfArticles if page.namespace()==0 and test not in page.getReferences()]

    for page in pagelist:

        temptime = None
#查找加上模板的日期, 并将过期的条目写入列表 

        notime=True
        for (id, timestamp, user, content) in page.fullVersionHistory():
        
            for template in templates:
                if template in (id, timestamp, user, content)[3] and datetime.datetime.utcnow() - templatetime(timestamp) > datetime.timedelta(days=30) and notime:
                    wikipedia.output(u'In [[%s]] Fond [[%s]], submit VFD' % (category, page.title()))
                    title=page.title()
                    title=u'\n==== [['+title+u']] ====\n'
                    vfdlist.append(page.title())
                    n=n+1
                    notime=False
#        wikipedia.output(u'[[%s]] Not more 30 days, KEEP' % ( page.title()))

    wikipedia.output(u'In [[%s]] Fond vfd totlal: %s' % (category, n))
    return vfdlist


def submitvfd(vfdlist,reson):
    """
	提交删除
    """
    todayvfd=u'Wikipedia:删除投票和请求/%s年%s月%s日' % (datetime.datetime.utcnow().year, datetime.datetime.utcnow().month, datetime.datetime.utcnow().day)
#    todayvfd=u'User:Shizhao/3'
    botc=u'Bot提交过期%s条目' % reson 
    vfdpage=wikipedia.Page(site,todayvfd)
    svfdlist=''.join(vfdlist) 
    if vfdpage.exists():
        text=vfdpage.get()
        text=text+u'\n=== 过期'+reson+u'条目 ===\n'+svfdlist+u'----\n超过30天仍然挂有{{tl|'+reson+u'}}模板的条目，由[[WP:BOT|]]自动提交。—~~~~'
        vfdpage.put(text, botc,  minorEdit=False )
    else:
        hear=u'{{subst:DRHead}}'
        text=hear+u'\n=== 过期'+reson+u'条目 ===\n'+svfdlist+u'----\n超过30天仍然挂有{{tl|'+reson+u'}}模板的条目，由[[WP:BOT|]]自动提交。—~~~~'
        vfdpage.put(text, botc+u'并建立删除请求页面', minorEdit=False )
    wikipedia.output(u'submit pages to VFD page: [[%s]] ' % todayvfd)

def vfdnotice(vfdlist, pagereson):
    """
    在提交删除的条目上挂删除模板
    """
    for title in vfdlist:
        page=wikipedia.Page(site,title)
        t=page.get()
        reson=u'{{Vfd|过期%s}}\n'% pagereson
        t=reson+t
        page.put(t, pagereson, minorEdit=True)	
        wikipedia.output(pagereson + u'：Bot提交删除')

def sleep(s):
    wikipedia.output(u'\nSleeping %s s' % s)	
    time.sleep(s)    
	
def substub():
    """
    过期小小作品提交删除
    """
    category=u'Category:%s月小小作品' % vfdm()
    templates=['{{Substub', '{{小小作品', '{{substub']
    reson=u'小小作品'

    vfdlist=vfd(category, templates)
    if len(vfdlist) >0:
        vfdnotice(vfdlist, reson)
        submitvfd(vfdlist,reson)
    else:
        wikipedia.output(u'Today Not Vfd!')
    sleep(60)


def Notability():
    """
    过期主题知名度不足条目
    """
    category=u'Category:%s月主題知名度不足的條目' % vfdm()
    templates=['{{知名度', '{{知名度不足', '{{fame', '{{Fame', '{{Notability', '{{notability']
    reson=u'知名度不足'

    vfdlist=vfd(category, templates)
    if len(vfdlist) >0:
        vfdnotice(vfdlist, reson)
        submitvfd(vfdlist,reson)
    else:
        wikipedia.output(u'：Today Not Vfd!')
    sleep(60)

def Importance():
    """
    过期重要性不足条目
    """
    category=u'Category:%s月重要性不明的條目' % vfdm()
    templates=['{{Importance', '{{importance', '{{重要性']
    reson=u'重要性不足'

    vfdlist=vfd(category, templates)
    if len(vfdlist) >0:
        vfdnotice(vfdlist, reson)
        submitvfd(vfdlist,reson)
    else:
        wikipedia.output(u'：Today Not Vfd!')
    sleep(60)

substub()
Notability()
Importance()
wikipedia.stopme() 
